package com.qidianit.mes.pro.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qidianit.mes.pojo.Material;
import com.qidianit.mes.pojo.pro.ProductionOrder;
import com.qidianit.mes.pojo.Result;
import com.qidianit.mes.pro.mapper.*;
import com.qidianit.mes.pro.service.ProductionOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.util.List;

@Service
public class ProductionOrderServiceImpl implements ProductionOrderService {

    @Autowired
    private ProductionOrderMapper productionOrderMapper;

    @Autowired
    private DictDataMapper dictDataMapper;
    @Autowired
    private CustomerMapper customerMapper;
    @Autowired
    private MaterialMapper materialMapper;
    @Autowired
    private UnitMapper unitMapper;

    /* ==================== 多条件查询==================== */
    @Override
    public Result<PageInfo<ProductionOrder>> list(ProductionOrder productionOrder,
                                                  Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<ProductionOrder> list = productionOrderMapper.selectList(productionOrder);

        for (ProductionOrder order : list) {
            order.setTypeDictData(dictDataMapper.selectById(order.getProductionOrderType()));
            order.setStatusDictData(dictDataMapper.selectById(order.getStatus()));
            order.setCustomer(customerMapper.selectByCode(order.getCustomerCode()));

            Material material = materialMapper.selectByCode(order.getMaterialCode());
            material.setUnit(unitMapper.selectById(material.getUnitId()));
            order.setMaterial(material);
        }

        PageInfo<ProductionOrder> pageInfo = new PageInfo<>(list);
        return Result.success(pageInfo);
    }
    /* ==================== 根据Id查询==================== */
    @Override
    public Result<ProductionOrder> selecById(Long id) {
        if (id == null) {
            return Result.error("id不能为空");
        }
        ProductionOrder productionOrder = productionOrderMapper.selectById(id);
        return productionOrder == null ? Result.error("工单不存在或已删除") : Result.success(productionOrder);
    }

    /* ==================== 新增工单==================== */
    @Override
    public Result<ProductionOrder> add(ProductionOrder productionOrder) {
        //1、简单校验
        if (productionOrder.getProductionOrderCode() == null ||
                productionOrder.getProductionOrderCode().trim().isEmpty()) {
            return Result.error("工单编号不能为空");
        }
        if (productionOrder.getProductionOrderName() == null ||
                productionOrder.getProductionOrderName().trim().isEmpty()) {
            return Result.error("工单名称不能为空");
        }
        if (productionOrder.getMaterialCode() == null ||
                productionOrder.getMaterialCode().trim().isEmpty()) {
            return Result.error("产品编号不能为空");
        }
        if (productionOrder.getCustomerCode() == null ||
                productionOrder.getCustomerCode().trim().isEmpty()) {
            return Result.error("客户编号不能为空");
        }
        //2填充默认值
        productionOrder.setStatus(1L);  //草稿
        productionOrder.setDeleteFlag(0);
        //二维码路径
        int rows = productionOrderMapper.insert(productionOrder);
        return rows == 1 ? Result.success(null) : Result.error("新增工单失败");
    }

    /* ==================== 根据工单编号编辑工单信息 ==================== */
    @Override
    public Result<ProductionOrder> updateById(ProductionOrder productionOrder) {
        //非空校验
        if (productionOrder.getId() == null || productionOrder.getProductionOrderCode() == null
                || productionOrder.getProductionOrderCode().trim().isEmpty()) {
            return Result.error("ID或工单编号不能为空");
        }
        //编号重复校验
        if (productionOrderMapper.countCodeExcludeSelf(productionOrder.getCustomerCode(), productionOrder.getId()) > 0) {
            return Result.error("工单编号重复");
        }
        int rows = productionOrderMapper.updateById(productionOrder.getId());
        return rows == 1 ? Result.error("编辑工单失败") : Result.success();
    }

    /* ==================== 根据工单编号删除工单信息 ==================== */
    @Override
    public Result<ProductionOrder> deleteById(Long id) {
        if (id == null) {
            return Result.error("ID不能为空");
        }
        int rows = productionOrderMapper.deleteById(id);
        return rows == 1 ? Result.success() : Result.error("删除工单失败");
    }


}